package fr.alma.domo.svc;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

import org.oasisopen.sca.annotation.AllowsPassByReference;
import org.oasisopen.sca.annotation.Service;

import edu.emory.mathcs.backport.java.util.Arrays;
import fr.alma.domo.model.AbstractService;
import fr.alma.domo.model.Collaboration;
import fr.alma.domo.model.CollaborationSchema;

@SuppressWarnings("serial")
@Service(CollaborationHandler.class)
@AllowsPassByReference
public class CollaborationHandlerImpl extends AbstractService implements CollaborationHandler {

	private static final Map<String,Collaboration> COLLABORATIONS 
		= Collections.synchronizedMap(new HashMap<String,Collaboration>());
	
	public CollaborationHandlerImpl() {
		super("CollaborationHandler");
	}

	@Override
	public void deleteInstance(String instanceId) {
		String[] splits = instanceId.split(":");
		COLLABORATIONS.get(splits[0]).deleteInstance(splits[1]);
	}

	@Override
	public String createSchema(Class<? extends CollaborationSchema> schema) {
		String schemaId = UUID.randomUUID().toString();
		COLLABORATIONS.put(schemaId, new Collaboration(schema));
		return schemaId;
	}

	@SuppressWarnings("unchecked")
	@Override
	public String createInstance(String schemaId, AbstractService... services) {
		return schemaId+":"+COLLABORATIONS.get(schemaId).newInstance(Arrays.asList(services));
	}
		
}
